home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / Effect library / Demo ƒ / Fades ƒ / Hilbert fade.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-11  |  3.8 KB  |  137 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        Hilbert wipe fade.c
  4.  
  5. Purpose:    Graphic effect to fade main window to solid pattern.
  6.             See comments below for more description.
  7.  
  8.  
  9. MSG Demo -- graphic effects demonstration program
  10. Copyright (C) 1992-4 Mark Pilgrim & Dave Blumenthal
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program in a file named "GNU General Public License".
  24. If not, write to the Free Software Foundation, 675 Mass Ave,
  25. Cambridge, MA 02139, USA.
  26.  
  27. \**********************************************************************/
  28.  
  29. #include "msg timing.h"
  30.  
  31. /* This fills the screen by a Hilbert space-filling curve, which is defined by
  32.    two mutually recursive drawing functions:
  33.    
  34.    X = left, Y, draw, right, X, draw, X, right, draw, Y, left
  35.    Y = right, X, draw, left, Y, draw, Y, left, draw, X, right
  36.    
  37.    Start by drawing X at the highest recursion level from the bottomleft of the
  38.    screen. (At recursion level 1, X and Y are null functions.)
  39. */
  40.  
  41. #define    RecursionLevel    4
  42. #define CorrectTime 1
  43. #define theWindowHeight (boundsRect.bottom-boundsRect.top)
  44. #define theWindowWidth (boundsRect.right-boundsRect.left)
  45.  
  46. pascal void HilbertFadeRecurse(Rect boundsRect, Pattern *thePattern,
  47.     int level, int whichpattern, int* x, int* y);
  48. pascal short HilbertFade(Rect boundsRect, Pattern *thePattern);
  49.  
  50. typedef char    Hilby[11];
  51.  
  52. static Hilby    HilbertPattern[]=
  53. {
  54.     {
  55.         0x02,0x69,0x00,0x01,0x42,0x00,0x42,0x01,0x00,0x69,0x02
  56.     },
  57.     {
  58.         0x01,0x42,0x00,0x02,0x69,0x00,0x69,0x02,0x00,0x42,0x01
  59.     }
  60. };
  61.  
  62. static int            HilbertDirection;
  63. static int            vBlockSize;
  64. static int            hBlockSize;
  65.  
  66. pascal void HilbertFadeRecurse(Rect boundsRect, Pattern *thePattern,
  67.     int level, int whichpattern, int* x, int* y)
  68. {
  69.     int                i;
  70.     Rect            source;
  71.     
  72.     for (i=0; i<11; i++)
  73.     {
  74.         switch (HilbertPattern[whichpattern][i])
  75.         {
  76.             case 0x01:     /* turn left */
  77.                 HilbertDirection--;
  78.                 if (HilbertDirection<0) HilbertDirection=3;
  79.                 break;
  80.             case 0x02:     /* turn right */
  81.                 HilbertDirection++;
  82.                 if (HilbertDirection==4) HilbertDirection=0;
  83.                 break;
  84.             case 0x00:    /* draw */
  85.                 StartTiming();
  86.                 SetRect(&source, *x, *y-vBlockSize, *x+hBlockSize, *y);
  87.                 OffsetRect(&source, boundsRect.left, boundsRect.top);
  88.                 SectRect(&source, &boundsRect, &source);
  89.                 FillRect(&source, *thePattern);
  90.                 switch (HilbertDirection)
  91.                 {
  92.                     case 0:
  93.                         *x+=hBlockSize;
  94.                         break;
  95.                     case 1:
  96.                         *y-=vBlockSize;
  97.                         break;
  98.                     case 2:
  99.                         *x-=hBlockSize;
  100.                         break;
  101.                     case 3:
  102.                         *y+=vBlockSize;
  103.                         break;
  104.                 }
  105.                 TimeCorrection(CorrectTime);
  106.                 break;
  107.             case 0x42:    /* call X */
  108.                 if (level>1)
  109.                     HilbertFadeRecurse(boundsRect,thePattern,level-1,0,x,y);
  110.                 break;
  111.             case 0x69:    /* call Y */
  112.                 if (level>1)
  113.                     HilbertFadeRecurse(boundsRect,thePattern,level-1,1,x,y);
  114.                 break;
  115.         }
  116.     }
  117. }
  118.  
  119. pascal short HilbertFade(Rect boundsRect, Pattern *thePattern)
  120. {
  121.     int            curx, cury;
  122.     int            answer, i;
  123.     
  124.     answer=1;
  125.     for (i=0; i<RecursionLevel; i++)
  126.         answer*=2;
  127.     vBlockSize=1+theWindowHeight/answer;    /* used to be 20 */
  128.     hBlockSize=1+theWindowWidth/answer;        /* used to be 32 */
  129.     HilbertDirection=0;
  130.     cury=theWindowHeight;
  131.     curx=0;
  132.     HilbertFadeRecurse(boundsRect,thePattern,RecursionLevel,0,&curx,&cury);
  133.     FillRect(&boundsRect, *thePattern);        /* in case we missed any bits */
  134.     
  135.     return 0;
  136. }
  137.